package com.amazon.web.controller;

import com.amazon.bo.ListOrdersResult;
import com.amazon.bo.Product;
import com.amazon.bo.Shop;
import com.amazon.bo.User;
import com.amazon.bo.enums.UserDayEnum;
import com.amazon.service.OrdersService;
import com.amazon.service.ProductService;
import com.amazon.service.ShopService;
import com.amazon.web.AuthRequired;
import com.amazon.web.controller.vo.AdminResult;
import com.amazon.web.controller.vo.ListOrdersResponse;
import com.amazon.web.controller.vo.Orders;
import com.amazon.web.model.PagedListModel;
import com.amazon.web.util.*;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import net.sf.json.JSON;
import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.amazon.web.util.SignatureUtil.*;

/**
 * Created by anhui on 2018/4/16.
 */
@RestController

@RequestMapping(value = "order", method = RequestMethod.POST)
public class OrderController {

    @Resource
    private ProductService productService;

    @Resource
    private ShopService shopService;

    @RequestMapping(value = "/order/list", method = RequestMethod.GET)
    public Object getOrders() throws Exception {

        List<String> list = new ArrayList<>();

        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t17635/245/662172844/395422/e1555191/5a9f5c97N77d3f46e.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t17881/211/707000235/154363/1249b522/5a9f5c8eN1e141582.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t15745/103/2293477831/488944/2c68fbb0/5a9f5d73N07824f0f.jpg");
        list.add("http://img14.360buyimg.com/popWaterMark/jfs/t19492/82/2546886816/195171/d4b55bd3/5afd300bNbc1aac60.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t17584/149/653682844/375181/7869fba4/5a9f5c95Ndedbc99e.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t14749/213/2425936784/288884/b752d8f/5a9f5c96Na323d642.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t15070/18/2410471032/476231/79d3403c/5a9f5c9fN44be4691.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t18172/59/646995684/330870/d58fde62/5a9f5ca5Nf13e9022.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t19474/67/666045105/148489/8504c8fb/5a9f5ca0Nd38aeb15.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t19222/128/675526211/88617/137c80be/5a9f5c9aN09d43e27.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t15220/82/2398136208/69137/ee6747b5/5a9f5c9dNf572dec8.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t14698/74/2460480464/137447/946376ec/5a9f5ca1Nc2e55cc7.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t17881/227/690374437/96202/4b23c7b6/5a9f5ca3Ndb5cf03c.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t15250/291/2472025515/142819/9d01a41c/5a9f5ca4Nd8f81114.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t14866/54/2422715569/136901/51a7a5b7/5a9f5caaNe3fe1ebd.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t15358/89/2421391303/327122/60613e7c/5a9f5cb0N386278f6.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t19606/320/629621224/130651/62b1490f/5a9f5ca8N18fc5e8b.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t16891/285/672793314/214265/5afcb4a9/5a9f5caaNd2712b39.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t17878/239/679630559/136428/1186b8f3/5a9f5cacN375b7ac6.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t13768/225/2628903981/409350/460bdbe6/5a9f5cb0Nceb55ef6.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t14809/37/2460450763/512312/999f9c9b/5a9f5d3fN6d883b18.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t14326/23/2444929788/370342/991ecd77/5a9f5d3bNcf21005d.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t16204/39/2257577632/219569/15b47c2/5a9f5d30N93c1ed56.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t17092/331/666617703/290146/6ca26dfd/5a9f5d2eNf6eeda82.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t17638/162/714910155/338502/a012bc2f/5a9f5d33Ncdf672ff.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t16801/342/656057564/328895/c9663a78/5a9f5d3aN28a45bcf.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t19393/138/654510869/297623/77b63c28/5a9f5d41N95bf38e5.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t16594/343/2192808134/353081/a776d850/5a9f5d3bN71f4917a.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t14323/36/2467344843/414201/2c760864/5a9f5d49N86e4b308.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t18178/357/655578125/251217/9cf625ec/5a9f5d4bNf1c300de.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t16018/237/2356217496/384160/b473d7ed/5a9f5d4cN6657c017.jpg");
        list.add("http://img30.360buyimg.com/popWaterMark/jfs/t17053/360/671467334/339022/895a59ec/5a9f5d4dN686a2d31.jpg");


        for(int i=0;i<list.size();i++) {
            HttpUtil.downloadImage(list.get(i),i+".jpg","c://java/pro");
        }

        return null;

    }


    @RequestMapping(value = "/upload", method = RequestMethod.GET)
    public Object addProduct(String productId) throws Exception {

        //Product product = productService.getById(595);
        //product.setDescription(delHTMLTag(product.getDescription()));

//        Product product1 = productService.getById(595);
//        product1.setDescription(delHTMLTag(product1.getDescription()));
//
//
//        Product product2 = productService.getById(588);
//        product2.setDescription(delHTMLTag(product2.getDescription()));

        //List<Product> list = new ArrayList<>();
        //list.add(product);
//        list.add(product1);
//        list.add(product2);

        long id = 27;
        Shop shop = shopService.getById(id);
        Map<String,Object> result= Tools.parserJson2Map(SignatureUtil.getFeedSubmissionResult("50001017754",shop));

        //Map<String,Object> result= Tools.parserJson2Map(SignatureUtil.uploadProduct(product,shop));

        //Map<String,Object> result1= Tools.parserJson2Map(SignatureUtil.uploadProduct(list,shop));

        //Map<String,Object> result2= Tools.parserJson2Map(SignatureUtil.uploadInventory(list,shop));

        //Map<String,Object> result3= Tools.parserJson2Map(SignatureUtil.uploadPrice(list,shop));

        //Map<String,Object> result4= Tools.parserJson2Map(SignatureUtil.uploadImage(list,shop));


        //Map<String,Object> result = new HashedMap();
        //result.put("1",result1);
        //result.put("2",result2);
        //result.put("3",result3);
        //result.put("4",result4);

        return  result;
    }

    public static String delHTMLTag(String htmlStr){

        htmlStr=htmlStr.replace("&lt;","<").replace("&gt;",">");

        String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式
        String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式
        String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式

        Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
        Matcher m_script=p_script.matcher(htmlStr);
        htmlStr=m_script.replaceAll(""); //过滤script标签

        Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
        Matcher m_style=p_style.matcher(htmlStr);
        htmlStr=m_style.replaceAll(""); //过滤style标签

        Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
        Matcher m_html=p_html.matcher(htmlStr);
        htmlStr=m_html.replaceAll(""); //过滤html标签

        return htmlStr.trim(); //返回文本字符串
    }


    @Resource
    private OrdersService ordersService;



    @RequestMapping(value = "admin/list", method = RequestMethod.GET)
    public Object getTyOrderList(int start, int length, int draw, String conn) {

        int total = ordersService.getOrdersCount(conn);
        AdminResult<Map<String, Object>> result = new AdminResult();
        result.setData(ordersService.getAdminOrders(start, length, conn));
        result.setDraw(draw);
        result.setRecordsTotal(total);
        result.setRecordsFiltered(total);
        return result;
    }

    @RequestMapping(value = "/orders", method = RequestMethod.GET)
    public Object orders() throws Exception {
        Shop shop = new Shop();
        String orders = SignatureUtil.getOrder(shop);

        Map<String,Object> orderMap=Tools.parserJson2Map(orders);


        List<Map<String,Object>> orderList =(List<Map<String,Object>>)((Map<String,Object>)((Map<String,Object>)orderMap.get("ListOrdersResult")).get("Orders")).get("Order");

        for(Map<String,Object> order :orderList){

            String orderStatus=order.get("OrderStatus").toString();
            if(!orderStatus.equals("Canceled")){

                Map<String,Object> shippingAddress =(Map<String,Object>)order.get("ShippingAddress");
                order.put("StateOrRegion",shippingAddress.get("StateOrRegion"));
                order.put("AddressLine1",shippingAddress.get("AddressLine1"));
                order.put("PostalCode",shippingAddress.get("PostalCode"));
                order.put("City",shippingAddress.get("City"));
                order.put("AddressType",shippingAddress.get("AddressType"));
                order.put("CountryCode",shippingAddress.get("CountryCode"));
                order.put("Name",shippingAddress.get("Name"));


                Map<String,Object> orderTotal =(Map<String,Object>)order.get("OrderTotal");
                order.put("CurrencyCode",orderTotal.get("CurrencyCode"));
                order.put("Amount",orderTotal.get("Amount"));

                Map<String,Object> paymentMethodDetails =(Map<String,Object>)order.get("PaymentMethodDetails");
                order.put("PaymentMethodDetail",paymentMethodDetails.get("PaymentMethodDetail"));


                com.amazon.bo.Orders od= new com.amazon.bo.Orders();
                od.setAmazonOrderId(order.get("AmazonOrderId").toString());
                od.setOrderBody(com.alibaba.fastjson.JSON.toJSONString(order));
                od.setPurchaseDate(order.get("PurchaseDate").toString());

                ordersService.addOrders(od);
            }
        }


        return orderMap;

    }


    @RequestMapping(value="getOrderList",method =RequestMethod.GET)
    public ApiListResponse getOrderList(int page,int pagesize,String key) {
        List<Map<String,Object>> list=ordersService.getProductList("", page, pagesize);
        int total = ordersService.getOrderCount("");
        //List<User> list = userService.getShopUserList(UserType.USER,"", page, pagesize);
        return ResponseUtil.getSuccessResponse(list, total);
    }


}
